﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core.ServiceModel;
using Microsoft.Practices.Unity;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 巡检路线下的巡检点操作
    /// </summary>
    /// Title:ZMCL92
    /// Service:ServiceZMCL92
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-6-28  高强   [创建]
    ///  
    public partial class ZMCL92 : EFViewCmes
    {
        public ZMCL92()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            _view.ViewType = "tree";
            _view.Args.Add("fdParentId", this.fdParentId.Value.ToString());
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击选择巡检点按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            TempButton = but;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("fdSurveyPointInfo", "");
            eiInfo.AddBlock(EIConstants.ResultBlock);
            HttpServiceAgent agent = new HttpServiceAgent();
            eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCL92");
            eiInfo.Attributes.Add(EIConstants.MethodNameKey, "queryAllSurveyPoint");
            agent.CallService(eiInfo, OnDoSelectCompleted);
        }

        /// <summary>
        /// 选择巡检点的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoSelectCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                base.OpenEFWindowAlert(e.EIInfo.DetailMsg, true);
                return;
            }

            if (e.EIInfo.GetBlock(EIConstants.ResultBlock).GetRowCount() == 0)
            {
                base.OpenEFWindowAlert("没有巡检点供选择", true);
                return;
            }
            SelectSurveyPoint selectSurveyPoint = new SelectSurveyPoint();
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.Header = "选择巡检点";
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能新增窗口大小
            _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterScreen;
            selectSurveyPoint.EIDataContext = new Platform.Core.UI.EIDataContext();
            selectSurveyPoint.EIDataContext.Update(e.EIInfo);
            _popup.Content = selectSurveyPoint;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(Window_Closed);
            _popup.ShowDialog();
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
                this.UpdateHomePageData(new EIInfo());//发布数据
            }
            //按钮生效
            base.TempButtonOpen(true);
        }

        /// <summary>
        /// 点击调整巡检顺序按钮，弹出页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup("调整",eiInfo, "巡检点管理-调整巡检点顺序", "update", null, sender, e);
        }

        /// <summary>
        /// 点击取消巡检点按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnDeleteButtonClick("取消",sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ChangeSurveyPointSerial changeSurveyPointSerial = new ChangeSurveyPointSerial(_dialogType);//构造一个新的修改页面
            changeSurveyPointSerial.beforeSerial.Value = this.grid.GetCheckedEIRows()[0]["fdPathSerial"].ToString();//传递修改前的顺序号
            changeSurveyPointSerial.EIDataContext = new Platform.Core.UI.EIDataContext();
            changeSurveyPointSerial.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = changeSurveyPointSerial;//让调整顺序页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(Window_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }
    }
}
